﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Text;
using XcoAppSpaces.Contracts.Serialization;
using BruteForceSerializer;
using BruteForceSerializer.Contract;
using XcoAppSpaces.Contracts.PortWiring;

namespace XcoAppSpaces.Serialization.Json
{
	/// <summary>
	/// XcoJsonSerializer serializes and desrializes messages in the Json format, making serialization
	/// plattform independent.
	/// </summary>
	public class XcoJsonSerializer : IXcoSerializer
	{
		private readonly ISerializer serializer = new Serializer();

		/// <summary>
		/// Adds a serialization surrogate to the serializer, that can be used to define special serialization
		/// mechanisms for certain types.
		/// </summary>
		/// <param name="surrogate">The surrogate to be added.</param>
		public void AddSurrogate(ISerializationSurrogate surrogate)
		{
			serializer.AddSurrogate(surrogate);
		}

		#region IXcoSerializer Members

		/// <summary>
		/// Deserializes an object from a byte array.
		/// </summary>
		/// <param name="element">The serialized object.</param>
		/// <returns>The object that has been deserialized.</returns>
		public object Deserialize(byte[] element)
		{
			string serialized = Encoding.UTF8.GetString(element, 0, element.Length-1);
			//Console.WriteLine("Deserializing: " + serialized);
			return serializer.DeSerialize(serialized);
		}

		/// <summary>
		/// Serializes an object into a byte array.
		/// </summary>
		/// <param name="element">The object to be serialized.</param>
		/// <returns>The serialized object.</returns>
		public byte[] Serialize(object element)
		{
			string serialized = serializer.Serialize(element);
			//Console.WriteLine("Serialized: " + serialized);
			return Encoding.UTF8.GetBytes(serialized);
		}

		/// <summary>
		/// Checks if the given type can be serialized. In case of the Json serializer, types must be
		/// public and have an empty default constructor.
		/// </summary>
		/// <param name="type">The type to be checked.</param>
		/// <param name="errorInfo">Contains information about serialization problems, if the type cannot be serialized.</param>
		/// <returns>True if the type can be serialized, otherwise false.</returns>
		public bool CanTypeBeSerialized(Type type, out string errorInfo)
		{
			return SerializableCheck.CanTypeBeSerialized(type, out errorInfo);
		}

		#endregion

		#region IXcoService Members

		/// <summary>
		/// Initializes the service.
		/// </summary>
		/// <param name="serviceRegistry">The service registry of the local space.</param>
		public void Initialize(XcoAppSpaces.Contracts.Service.IXcoServiceRegistry serviceRegistry)
		{
			serializer.AddSurrogate(new PortSerializationSurrogate(serviceRegistry.Resolve<IPortWirer>()));
		}

		#endregion
	}
}
